// Evol scripts.
// Author:
//    Travolta
// Description:
//    Pious flying around town
// Variables:
//    none

function	script	ArtisFlyingPiouLogic	{
    .@tick = gettimetick(1);
    .@LastTimeTouched = getvariableofnpc(.LastTimeTouched, strnpcinfo(3));
    if (.@tick > .@LastTimeTouched + 300)
    {
        .speed = 200;
        set getvariableofnpc(.LastPlayerTouched, strnpcinfo(3)), -1;
    }
    .@rnd = rand(5) - 3;
    if (.@rnd > 0)
        return execmovecmd("wait " + .@rnd);
    return 0;
}

001-1,53,117,0	script	#FlyingPiou1	NPC_FLYING_PIOU,{

    function  TryCatchPiou {
        .@agi = readparam(bAgi);
        @ArtisQuests_CatchPiouTries += 1;
        getmapxy(.@map$, .@x1, .@y1, 1);
        getmapxy(.@map$, .@x2, .@y2, 0);
        .@distance = distance(.@x1, .@y1, .@x2, .@y2);
        .@chance = max(ArtisQuests_CatchPiou_Difficulcy,
                       20 + .@distance * 5 - .@agi/10 -
                       @ArtisQuests_CatchPiouTries);
        return rand(.@chance);
    }

    .@charid = getcharid(0);
    if (.LastPlayerTouched <= 0)
    {
        .LastPlayerTouched = .@charid;
        @ArtisQuests_CatchPiouTries = 0;
    }
    else if (.LastPlayerTouched != .@charid)
    {
        message strcharinfo(0), l("Hm, it seems another player is trying to catch the piou, I shouldn't interfere.");
        end;
    }

    .@q = getq(ArtisQuests_CatchPiou);
    if (.@q != 1)
    {
        mesn "Narrator";
        mes col(l("You scare the piou, but let it go."), 9);
        close;
    }

    .@tick = gettimetick(1);
    if (.@tick < .LastTimeTouched + min(4, ArtisQuests_CatchPiou_Difficulcy
                                        + @ArtisQuests_CatchPiouTries / 3))
    {
        message strcharinfo(0), l("Dang, I scared it! More patience, @@, more patience.", strcharinfo(0));
        .LastTimeTouched = .@tick;
        end;
    }

    .LastTimeTouched = .@tick;

    .@rnd = TryCatchPiou();
    if (!.@rnd)
    {
        npcstop;
        stopnpctimer;
        getmapxy(.@map$, .@x, .@y, 1);
        npcwalkto .@x, .@y;
        .@trader$ = "Salem#001-1";
        mesn "Narrator";
        mes col(l("You caught the piou, but it's trying to escape from you. You'd better hurry back to Salem."), 9);
        set getvariableofnpc(.LastPiouHunter$, .@trader$), strcharinfo(0);
        set getvariableofnpc(.PiouEscapedMessage$, .@trader$), l("Oh no, the piou escaped!");
        set getvariableofnpc(.LastPiouHunterID, .@trader$), .@charid;
        set getvariableofnpc(.PiouCaught, .@trader$), 1;
        donpcevent .@trader$ + "::OnPiouFlee";
        next;
        disablenpc strnpcinfo(3);
        close;
    }
    else
    {
        setarray .RandomFailureMessages$[0], l("So close!"), l("It escaped!"), l("Almost got it!"), l("Oh, the little...");
        .@r = rand(getarraysize(.RandomFailureMessages$));
        message strcharinfo(0), .RandomFailureMessages$[.@r];
        .speed = max(140, 200 - 10 * (@ArtisQuests_CatchPiouTries +
                     ArtisQuests_CatchPiou_Difficulcy));
    }
    end;

OnHour00:
    .LastTimeTouched = 0;
    end;

OnTimer1000:
    dographmovestep;

OnInit:
    .sex = G_OTHER;
    .distance = 5;
    // .debug = 1;

    initmovegraph "market_start",    53,  117,
                  "fountain",        79,  75,  102, 91,
                  "river_bank_1",    107, 94,  117, 102,
                  "river_bank_2",    117, 110,
                  "chelios_right",   100, 112, 113, 118,
                  "chelios_left",    80,  95,  93,  111,
                  "market_right",    73,  114, 83,  121,
                  "market_center",   50,  113, 66,  121,
                  "market_left",     36,  120, 42,  128,
                  "batiment_inside", 48,  197, 65,  106,
                  "batiment_behind", 33,  90,  41,  103,
                  "library_front",   42,  82,  63,  90,
                  "taree_yard",      69,  82,
                  "taree_behind",    61,  62,  76,  71,
                  "townhall_behind", 72,  40,  93,  55,
                  "townhall_right",  99,  55,  106, 60,
                  "inn_behind",      107, 63,  123, 76
                  ;

    setmovegraphcmd "fountain",       "river_bank_1",   1, "call ArtisFlyingPiouLogic",
                    "river_bank_1",   "river_bank_2",   1, "call ArtisFlyingPiouLogic",
                    "river_bank_2",   "chelios_right",  2, "call ArtisFlyingPiouLogic",
                    "chelios_right",  "chelios_left",   1, "call ArtisFlyingPiouLogic",
                    "chelios_left",   "fountain",       1, "wait 1; call ArtisFlyingPiouLogic",
                    "chelios_left",   "market_right",   1, "call ArtisFlyingPiouLogic",
                    "chelios_right",  "market_right",   1, "call ArtisFlyingPiouLogic",
                    "market_right",   "market_center",  1, "call ArtisFlyingPiouLogic",
                    "market_center",  "batiment_inside",1, "call ArtisFlyingPiouLogic",
                    "batiment_inside","library_front",  1, "call ArtisFlyingPiouLogic",
                    "library_front",  "taree_yard",     1, "call ArtisFlyingPiouLogic",
                    "library_front",  "taree_behind",   1, "call ArtisFlyingPiouLogic",
                    "library_front",  "batiment_behind",1, "call ArtisFlyingPiouLogic",
                    "batiment_behind","market_left",    1, "wait 3; call ArtisFlyingPiouLogic",
                    "market_left",    "market_center",  1, "call ArtisFlyingPiouLogic",
                    "taree_yard",     "fountain",       1, "call ArtisFlyingPiouLogic",
                    "taree_behind",   "townhall_behind",1, "call ArtisFlyingPiouLogic",
                    "townhall_behind","townhall_right", 1, "call ArtisFlyingPiouLogic",
                    "townhall_right", "inn_behind",     1, "call ArtisFlyingPiouLogic",
                    "inn_behind",     "fountain",       1, "wait 2; call ArtisFlyingPiouLogic",
                    "inn_behind",     "river_bank_1",   1, "call ArtisFlyingPiouLogic",
                    "market_start",   "batiment_inside",1, "call ArtisFlyingPiouLogic"
                    ;

    firstmove "speed 200", "market_start";
    initnpctimer;

    .LastPlayerTouched = -1;
    end;
}
